/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.qdu.dao.impl;

import com.qdu.dao.MovieDao;
import com.qdu.entity.Movie;
import com.qdu.util.DatabaseUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Lenovo
 */
public class MovieDaoImpl extends BaseDaoImpl2 implements MovieDao  {

    @Override
    public int insert(Movie m) {
         return executeUpdate("insert into Movie values(?,?,?,?,?,?,?,?,?,?,?,?)",
                 m.getMovieId(),
                 m.getMchineseName(),
                 m.getMoriginalName(),
                 m.getMtype(),
                 m.getMreleaseDate(),
                 m.getMlanguage(),
                 m.getMcountry(),
                 m.getMlength(),
                 m.getMdigest(),
                 m.getMcover(),
                 m.getMdirector(),
                 m.getMrating());
                 
    }

    @Override
    public int update(Movie m) {
        return executeUpdate("update Movie set MChineseName=?,MOriginalName=? ,MType=?,MReleaseDate=?,MLanguage=? ,MCountry=?,Mdirector=? where MovieId=?",
                m.getMchineseName(),m.getMoriginalName(),m.getMtype(),m.getMreleaseDate(),m.getMlanguage(),m.getMcountry(),m.getMdirector(),m.getMovieId());
    }

    @Override
    public int deleteByMovieId(String MovieId) {
         return executeUpdate("delete Movie where MovieId=?",MovieId);
    }
    
    
 /**
     * 获取所有电影的基本信息，包括电影编号、名称、评分和图片
     *
     * @return 一个Movie列表，包含所有电影基本信息
     */
    @Override
    public List<Movie> getAllMovies() {
         Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Movie> list = new ArrayList<>();
        
        try {
            con = DatabaseUtil.getConnection();
            ps = con.prepareStatement("select MovieId,MChineseName,MRating,MCover from Movie");
            rs = ps.executeQuery();
            while (rs.next()) {
                list.add(new Movie(rs.getString(1), rs.getString(2), rs.getFloat(3), rs.getString(4)));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }
        return list;
    }
  /**
     * 根据电影编号查询电影信息
     *
     * @param movieId 字符串表示的电影编号
     * @return 一个Movie对象，包含一个电影的所有信息
     */
    @Override
    public Movie getMovieInfoById(String movieId) {
         Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        
        try {
            con = DatabaseUtil.getConnection();
            ps = con.prepareStatement("select * from Movie where MovieId=?");
            ps.setString(1, movieId);
            rs = ps.executeQuery();
            while (rs.next()) {
                return new Movie(
                        rs.getString(1),
                        rs.getString(2),
                        rs.getString(3),
                        rs.getString(4),
                        rs.getString(5),
                        rs.getString(6),
                        rs.getString(7),
                        rs.getInt(8),
                         rs.getString(9),
                         rs.getString(10),
                         rs.getString(11),
                         rs.getFloat(12)
                       );
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }
        return null;
    }

    @Override
    //刘萌
    public List<Movie> getMoviesByKeyword(String movieName) {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Movie> list = new ArrayList<>();
        
        try {
            con = DatabaseUtil.getConnection();
            ps = con.prepareStatement("select MovieId,MChineseName,MRating,MCountry,Mdirector from Movie where MChineseName like ?");
            ps.setString(1, "%" + movieName + "%");
            rs = ps.executeQuery();
            while (rs.next()) {
                list.add(new Movie(rs.getString(1), rs.getString(2), rs.getFloat(3), rs.getString(4), rs.getString(5)));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }
        return list;
    }
    }

  
    

